package com.zjj.zonghe.test1;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date 2025/4/20
 */
public class Test2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int size;
        while (true) {
            System.out.print("请输入棋盘的大小,至少为5: ");
            if (scanner.hasNextInt()) {
                size = scanner.nextInt();
                if (size >= 5) {
                    break;
                } else {
                    System.out.println("输入不符合要求，请重新输入: ");
                }
            } else {
                System.out.println("输入类型错误，请输入一个整数。");
                scanner.next();
            }
        }

        char[][] board = new char[size][size];
        initBoard(board);
        System.out.println("即将开局");
        printBoard(board);

        boolean gameOver = false;
        while (!gameOver) {
            // 黑方落子
            if (!placePiece(scanner, board, size, '@')) {
                continue;
            }
            if (checkWin(board, size, '@')) {
                System.out.println("黑方获胜！游戏结束。");
                gameOver = true;
                continue;
            }
            if (isBoardFull(board)) {
                System.out.println("棋盘已满，平局！游戏结束。");
                gameOver = true;
                continue;
            }

            // 白方落子
            if (!placePiece(scanner, board, size, 'O')) {
                continue;
            }
            if (checkWin(board, size, 'O')) {
                System.out.println("白方获胜！游戏结束。");
                gameOver = true;
                continue;
            }
            if (isBoardFull(board)) {
                System.out.println("棋盘已满，平局！游戏结束。");
                gameOver = true;
                continue;
            }
        }
    }

    // 初始化棋盘
    public static void initBoard(char[][] board) {
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board.length; j++) {
                board[i][j] = '*';
            }
        }
    }

    // 打印棋盘
    public static void printBoard(char[][] board) {
        char startChar = 'a';
        for (int i = 0; i < board.length; i++) {
            System.out.print((char) (startChar + i) + "  ");
            for (int j = 0; j < board.length; j++) {
                System.out.print(board[i][j] + "  ");
            }
            System.out.println();
        }
        System.out.print("   ");
        for (int i = 0; i < board.length; i++) {
            System.out.print((char) (startChar + i) + "  ");
        }
        System.out.println();
    }

    // 判断输入是否有效
    public static boolean isValidInput(String input, int size) {
        if (input.length() != 2) {
            return false;
        }
        char colChar = input.charAt(0);
        char rowChar = input.charAt(1);
        if (colChar < 'a' || colChar - 'a' >= size || rowChar < 'a' || rowChar - 'a' >= size) {
            return false;
        }
        return true;
    }

    // 将输入坐标转换为数组下标
    public static int[] getCoords(String input) {
        int col = input.charAt(0) - 'a';
        int row = input.charAt(1) - 'a';
        return new int[]{row, col};
    }

    // 落子方法
    public static boolean placePiece(Scanner scanner, char[][] board, int size, char piece) {
        while (true) {
            System.out.println("请" + (piece == '@'? "黑方" : "白方") + "下子: ");
            String input = scanner.next();
            if (!isValidInput(input, size)) {
                System.out.println("输入无效，请重新输入: ");
                continue;
            }
            int[] coords = getCoords(input);
            if (board[coords[0]][coords[1]] != '*') {
                System.out.println("该位置已被占用，请重新下子");
                continue;
            }
            board[coords[0]][coords[1]] = piece;
            printBoard(board);
            return true;
        }
    }

    // 检查是否获胜
    public static boolean checkWin(char[][] board, int size, char piece) {
        // 检查横向
        for (int i = 0; i < size; i++) {
            for (int j = 0; j <= size - 5; j++) {
                boolean win = true;
                for (int k = 0; k < 5; k++) {
                    if (board[i][j + k] != piece) {
                        win = false;
                        break;
                    }
                }
                if (win) {
                    return true;
                }
            }
        }

        // 检查纵向
        for (int j = 0; j < size; j++) {
            for (int i = 0; i <= size - 5; i++) {
                boolean win = true;
                for (int k = 0; k < 5; k++) {
                    if (board[i + k][j] != piece) {
                        win = false;
                        break;
                    }
                }
                if (win) {
                    return true;
                }
            }
        }

        // 检查正斜向
        for (int i = 0; i <= size - 5; i++) {
            for (int j = 0; j <= size - 5; j++) {
                boolean win = true;
                for (int k = 0; k < 5; k++) {
                    if (board[i + k][j + k] != piece) {
                        win = false;
                        break;
                    }
                }
                if (win) {
                    return true;
                }
            }
        }

        // 检查反斜向
        for (int i = 0; i <= size - 5; i++) {
            for (int j = 4; j < size; j++) {
                boolean win = true;
                for (int k = 0; k < 5; k++) {
                    if (board[i + k][j - k] != piece) {
                        win = false;
                        break;
                    }
                }
                if (win) {
                    return true;
                }
            }
        }

        return false;
    }

    // 检查棋盘是否已满
    public static boolean isBoardFull(char[][] board) {
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board.length; j++) {
                if (board[i][j] == '*') {
                    return false;
                }
            }
        }
        return true;
    }
}